/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.wicket.interfaces;

import com.myapp.wicket.data.MockDataBaseDAO;
import com.myapp.wicket.data.MockDataBaseDAO.Country;
import java.util.Iterator;
import java.util.List;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;

/**
 * CountryDataProviderExample
 * 
 * Wraps the numbers List which is declared as transient.
 * 
 * When Wicket calls detach the numbers list will be set to null
 * and thus it will not be included when the CountryDataProviderExample
 * instance is serialized.
 * 
 * @author Jeff
 */
public class CountryDataProviderExample implements IDataProvider<MockDataBaseDAO.Country> {

    private transient List<Country> countries;
    
    private class CountryLDM extends LoadableDetachableModel<Country>{
        private int id;

        public CountryLDM(Country country) {
            super(country);
            this.id = country.getId();
        }
        
        @Override
        protected Country load() {
            System.out.println("CountryLDM::load called");
            return MockDataBaseDAO.getMockDataBaseDAO().getCountries().get(id);
        }
        
    } 
    
    @Override
    public Iterator<? extends Country> iterator(int first, int count) {
        return countries.subList(first, first + count).iterator();
    }

    @Override
    public int size() {
        countries = MockDataBaseDAO.getMockDataBaseDAO().getCountries();
        return countries.size();
    }

    @Override
    public IModel<Country> model(Country country) {
        return new CountryLDM(country);
    }

    @Override
    public void detach() {
        countries = null;
    }
    
}
